package com.boot.framework.config;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Objects;

@Configuration
public class SaTokenConfig implements WebMvcConfigurer {
    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry
                .addInterceptor(new SaInterceptor(handle -> {
                    SaRouter.notMatch("*.jpg", "png", "*.html", "*.css", "*.js");
//                    StpUtil.checkLogin();
                    String token = SaHolder.getRequest().getHeader(SaSameUtil.SAME_TOKEN);
                    if (Objects.isNull(token)){
                        StpUtil.checkLogin();
                    }else {
                        SaSameUtil.checkToken(token);
                    }

                }))
                .addPathPatterns("/**")
                .excludePathPatterns("/**/login","/**/druid/**", "/**/swagger-ui/**", "/doc.html/**",
                        "/v3/api-docs/**", "/webjars/js/**", "/webjars/css/**", "/static/**", "/favicon.ico")
                ;
    }
}
